package com.zjw.zy.service.helper;

import com.zjw.zy.bean.HourlyWeather;
import com.zjw.zy.bean.NearWeather;
import com.zjw.zy.bean.Weather15Day;
import com.zjw.zy.bean.Weather1Day;
import com.zjw.zy.service.net.HttpService;
import com.zjw.zy.smi.JustWithIt;
import com.zjw.zy.utils.AppUtils;
import com.zjw.zy.utils.LogUtils;
import com.zjw.zy.utils.NotificationUtils;
import com.zjw.zy.utils.TimeUtils;
import com.zjw.zy.utils.taskpool.TaskPool;

import java.util.ArrayList;
import java.util.List;

import remotecall.RemoteCallManager;
import remotecall.call.IRemoteCall;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/17 13:34
 */
public final class WeatherHelper extends BaseHelper {

    private static final String TAG = "WeatherHelper";

    private final List<IRemoteCall<Weather1Day>> mHourWeatherObservers = new ArrayList<>();
    private final List<IRemoteCall<NearWeather>> mNearWeatherObservers = new ArrayList<>();

    private static volatile WeatherHelper instance;

    private int hour;

    private Weather1Day todayWeather = null;

    public Weather1Day getTodayWeather() {
        return todayWeather;
    }

    private void considerNotify(Weather1Day weather1Day) {
        if (!TimeUtils.isDay()){
            LogUtils.d(TAG,"is day , not bear weather notify");
            return;
        }
        List<HourlyWeather> hourlyFcsts = weather1Day.hourlyFcsts;
        //当前天气已经下雨，不通知
        if (hourlyFcsts.get(0).text.contains("雨")) {
            return;
        }
        //三小时的特殊天气通知
        for (int i = 1; i <= Math.min(3, hourlyFcsts.size()); i++) {
            HourlyWeather weather = hourlyFcsts.get(i);
            if (weather.text.contains("雨")) {
                NearWeather nearWeather = new NearWeather(i + "小时后" + weather.text, "洋洋~快下雨了~准备好雨伞", i, weather);
                for (IRemoteCall<NearWeather> observer : mNearWeatherObservers) {
                    observer.call(nearWeather);
                }
                return;
            }
        }
    }

    public static WeatherHelper getInstance() {
        if (instance == null) {
            synchronized (WeatherHelper.class) {
                if (instance == null) {
                    instance = new WeatherHelper();
                }
            }
        }
        return instance;
    }

    private WeatherHelper() {
        hour = TimeUtils.currentTime().getHour();
        //每小时自动刷新天气
        TimeUtils.registerTimeObserver(date -> {
            if (hour == date.getHour()) {
                return;
            }
            hour = date.getHour();
            request1DayWeather(RemoteCallManager.defaultImpl().remoteCall(weather1Day -> {
                todayWeather = weather1Day;
                synchronized (mHourWeatherObservers) {
                    for (IRemoteCall<Weather1Day> observer : mHourWeatherObservers) {
                        observer.call(weather1Day);
                    }
                }
                considerNotify(weather1Day);
            }));
        });
        //发送天气通知，雨
        observerNearWeather(RemoteCallManager.defaultImpl().remoteCall(obj -> {
            NotificationUtils.sendNearWeatherNotification(AppUtils.getService(), obj);
        }));

        //每天早上推送天气
        TimeUtils.addTimeScheduler(7, 30, () ->
                NotificationUtils.sendWeatherNotification(requireService(), getTodayWeather()));


        //每天早上推送日子
        TimeUtils.addTimeScheduler(7, 31, () ->
                NotificationUtils.sendDayNotification(requireService()));
    }

    public void request15DayWeather(JustWithIt<Weather15Day> call) {
        TaskPool.concurrent().post(() -> HttpService.request15Weather(call));
    }

    public void request1DayWeather(IRemoteCall<Weather1Day> call) {
        TaskPool.concurrent().post(() -> HttpService.requestHourlyWeather(obj -> {
            todayWeather = obj;
            call.call(obj);
        }));
    }


    public void observer1DayWeather(IRemoteCall<Weather1Day> call) {
        synchronized (mHourWeatherObservers) {
            mHourWeatherObservers.add(call);
        }
    }

    public void observerNearWeather(IRemoteCall<NearWeather> call) {
        synchronized (mNearWeatherObservers) {
            mNearWeatherObservers.add(call);
        }
    }

    public void remove1DayWeather(IRemoteCall<Weather1Day> call) {
        synchronized (mHourWeatherObservers) {
            mHourWeatherObservers.remove(call);
        }
    }

    public void removeNearWeather(IRemoteCall<NearWeather> call) {
        synchronized (mNearWeatherObservers) {
            mNearWeatherObservers.remove(call);
        }
    }


}
